<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use think\facade\Request;
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Validate;
use think\facade\Db;
use think\Response\Json;


class Comment extends BaseController
{
    /**
     * 获取用户评论列表
     * @return Response
     */
    public function index(): Json
    {
        // 验证Bearer Token
        $token = Request::header('Authorization');
        if (empty($token) || !preg_match('/^Bearer\s+(.+)$/i', $token, $matches)) {
            return json(['code' => 401, 'msg' => '未授权访问']);
        }
    
        $token = $matches[1];
        $user = Db::name('think_users')->where('token', $token)->find();
        if (!$user) {
            return json(['code' => 401, 'msg' => 'token无效']);
        }

        // 获取目标用户ID
        $targetUserId = $this->request->param('id/d');
        if (!$targetUserId) {
            return json(['code' => 400, 'msg' => '用户ID不能为空']);
        }

        // 检查目标用户是否存在
        $targetUser = Db::name('think_users')
            ->where('id', $targetUserId)
            ->where('status', 1)
            ->find();

        if (!$targetUser) {
            return json(['code' => 404, 'msg' => '用户不存在或已被禁用']);
        }

        // 检查当前用户是否被目标用户拉黑
        $isBlocked = Db::name('user_blacklist')
            ->where('user_id', $targetUserId)
            ->where('blocked_user_id', $currentUserId)
            ->where('status', 1)
            ->find();

        if ($isBlocked) {
            return json(['code' => 403, 'msg' => '您已被该用户拉黑，无法查看评论']);
        }

        // 获取分页参数
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 10);
        
        // 验证分页参数
        if ($page < 1) $page = 1;
        if ($limit < 1 || $limit > 50) $limit = 10;

        // 查询评论列表
        $query = Db::name('think_comments')
            ->alias('c')
            ->join('think_users u', 'c.commenter_id = u.id')
            ->where('c.commented_id', $targetUserId)
            ->field([
                'c.id',
                'c.commenter_id',
                'c.commented_id',
                'c.content',
                'c.images',
                'c.create_time',
                'c.update_time',
                'u.nickname',
                'u.avatar',
                'c.score',
                'u.is_verified'
            ])
            ->order('c.create_time', 'desc');

        // 获取总数
        $total = $query->count();

        // 获取分页数据
        $list = $query->page($page, $limit)->select()->toArray();

        // 处理评论数据
        foreach ($list as &$item) {
            // 处理图片数据
            $item['images'] = $item['images'] ? json_decode($item['images'], true) : [];
            $item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
            $item['update_time'] = date('Y-m-d H:i:s', strtotime($item['update_time']));
        }

        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => [
                'total' => $total,
                'page' => $page,
                'limit' => $limit,
                'list' => $list
            ]
        ]);
    }
}